package com.controller;

import com.bean.UserBean;
import com.jpa.UserJPA;
import com.util.StringUtil;
import com.util.dynamicquery.DynamicQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "自定义sql查询 ")
@Slf4j
public class 自定义Sql查询 {
    @Autowired
    private UserJPA userJPA;
    @ApiOperation("批量添加")
    @GetMapping("/batchAdd")
    public String batchAdd(){
        Integer maxId  = userJPA.getMaxId().intValue();
        maxId = StringUtil.isNull(maxId,1);
        for(int i = maxId;i<maxId+10;i++){

            UserBean userBean = UserBean.builder()
                    .name(String.format("黄帝%s号",i))
                    .age(i)
                    .address(String.format("天府三街%s号",i))
                    .build();
            userJPA.save(userBean);
        }

        return "添加成功";

    }
    @GetMapping("delByName")
    @ApiOperation("根据名称删除")
    public String delete(String name){
       int i =  userJPA.delByName(name);
       log.info("删除条数:{} 条",i);
        return "删除成功,删除条数:"+i+"条";
    }
    @GetMapping("/queryAll")
    @ApiOperation("查询所有")
    public List<UserBean> queryAll(){
        return userJPA.findAll();
    }
    @Autowired
    private DynamicQuery dynamicQuery;
    private String delSql="delete from user where address like concat('%',?1,'%')";
    @ApiOperation("根据地址删除")
    @GetMapping("/delByAddress")
    @Transactional
    public String delByStreet(String address){
        int i = dynamicQuery.nativeExecuteUpdate(delSql,address);
        return String.format("删除%s条",i);
    }


}
